
*Set working directory
cd ""

use datasets\protak_data.dta, clear 

*Create ideology var from liberal and relright dummy
gen ideology = .
replace ideology = 1 if liberal == 1
replace ideology = 2 if relright == 1
replace ideology = 0 if liberal == 0 & relright == 0
label define ideology 0 "Other" 1 "Liberal Union" 2 "Religious Right"
label values ideology ideology

* "For the minority of candidates (45) who did not participate in an election between 1888 and April 1894, I instead use the average margin over the threshold of the strongest prior co-partisan candidate(s) in the district in which they competed."
replace avg_margin_pre1894 = avg_libmargin if avg_margin_pre1894 == . & ideology == 1
replace avg_margin_pre1894 = avg_rrmargin if avg_margin_pre1894 == . & ideology == 2

*Mean center electoral security
sum avg_margin_pre1894, meanonly
gen c_elecsec = avg_margin_pre1894 - r(mean)

*Analyses
estimates clear 

global controls c_share_enfranchised nobility incumbent secure  

eststo m_liberal: logit pro_tak ///
    i.liberal ///
    c.c_wealth ///
    c.c_elecsec ///
    i.liberal#c.c_wealth ///
    i.liberal#c.c_elecsec ///
    c.c_wealth#c.c_elecsec ///
    i.liberal#c.c_wealth#c.c_elecsec ///
    i.relright ///
    c.c_petition ///
    $controls, vce(robust)
vif, uncentered	

eststo m_religious: logit pro_tak ///
    i.relright ///
    c.c_petition ///
    c.c_elecsec ///
    i.relright#c.c_petition ///
    i.relright#c.c_elecsec ///
    c.c_petition#c.c_elecsec ///
    i.relright#c.c_petition#c.c_elecsec ///
    i.liberal ///
    c.c_wealth ///
    $controls, vce(robust)
vif, uncentered
				
eststo m_combined: logit pro_tak ///
    i.liberal ///
    i.relright ///
    c.c_wealth ///
    c.c_petition ///
    c.c_elecsec ///
    i.liberal#c.c_wealth ///
    i.relright#c.c_petition ///
    i.liberal#c.c_elecsec ///
    i.relright#c.c_elecsec ///
    c.c_wealth#c.c_elecsec ///
    c.c_petition#c.c_elecsec ///
    i.liberal#c.c_wealth#c.c_elecsec ///
    i.relright#c.c_petition#c.c_elecsec ///
    $controls, vce(robust)
vif, uncentered



eststo m_combined_threatland: logit pro_tak ///
    i.liberal ///
    i.relright ///
    c.c_wealth ///
    c.c_petition ///
    c.c_elecsec ///
    i.liberal#c.c_wealth ///
    i.relright#c.c_petition ///
    i.liberal#c.c_elecsec ///
    i.relright#c.c_elecsec ///
    c.c_wealth#c.c_elecsec ///
    c.c_petition#c.c_elecsec ///
    i.liberal#c.c_wealth#c.c_elecsec ///
    i.relright#c.c_petition#c.c_elecsec ///
    $controls c_tot_strikes c_share_manufacturing c_landineq c_share_landowners, vce(robust)
vif, uncentered

*Show Table 
esttab m*   , ///
    eform eqlabels(none) b(3) se(3) ///
    star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
    varwidth(40) pr2 nomtitle nonote ///
    drop(*0.* _cons) ///
    order(1.liberal 1.relright c_wealth c_petition c_elecsec ///
          1.liberal#c.c_wealth 1.relright#c.c_petition ///
          1.liberal#c.c_elecsec 1.relright#c.c_elecsec ///
          c.c_wealth#c.c_elecsec c.c_petition#c.c_elecsec ///
          1.liberal#c.c_wealth#c.c_elecsec ///
          1.relright#c.c_petition#c.c_elecsec ///
		  c_tot_strikes c_share_manufacturing c_landineq c_share_landowners ///
          c_share_enfranchised nobility incumbent secure _cons) ///
		  label ///
    coeflabel( ///
          1.liberal "Liberal" ///
          1.relright "Religious" ///
          c_wealth "District wealth" ///
          c_petition "Petition support" ///
          c_elecsec "Electoral security" ///
          1.liberal#c.c_wealth "Liberal $\times$ Wealth" ///
          1.relright#c.c_petition "Religious $\times$ Petition" ///
          1.liberal#c.c_elecsec "Liberal $\times$ Elec. security" ///
          1.relright#c.c_elecsec "Religious $\times$ Elec. security" ///
          c.c_wealth#c.c_elecsec "Wealth $\times$ Elec. security" ///
          c.c_petition#c.c_elecsec "Petition $\times$ Elec. security" ///
          1.liberal#c.c_wealth#c.c_elecsec "Liberal $\times$ Wealth $\times$ Elec. security" ///
          1.relright#c.c_petition#c.c_elecsec "Religious $\times$ Petition $\times$ Elec. security" ///
          c_share_enfranchised "\% of men enfranchised" ///
          nobility "Aristocratic (1=yes)" ///
          incumbent "Incumbent (1=yes)" ///
          secure "Uncontested (1=yes)" ) ///
    refcat(c_tot_strikes "\textbf{Threat and Land}" c_share_enfranchised "\textbf{Controls}", nolab) ///
    stats(r2_p ll aic bic chi2 N, ///
          labels("Pseudo \text{R}^{2}" "Log-Likelihood" "AIC" "BIC" "Wald-Chi^{2}" "Observations") ///
          fmt(3 3 3 3 3 0)) 
		
*Export Table 	
esttab m*   using output/elecincentives2.tex , replace ///
    eform eqlabels(none) b(3) se(3) ///
    star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
    varwidth(40) pr2 nomtitle nonote ///
    drop(*0.* _cons) ///
    order(1.liberal 1.relright c_wealth c_petition c_elecsec ///
          1.liberal#c.c_wealth 1.relright#c.c_petition ///
          1.liberal#c.c_elecsec 1.relright#c.c_elecsec ///
          c.c_wealth#c.c_elecsec c.c_petition#c.c_elecsec ///
          1.liberal#c.c_wealth#c.c_elecsec ///
          1.relright#c.c_petition#c.c_elecsec ///
		  c_tot_strikes c_share_manufacturing c_landineq c_share_landowners ///
          c_share_enfranchised nobility incumbent secure _cons) ///
		  label ///
    coeflabel( ///
          1.liberal "Liberal" ///
          1.relright "Religious" ///
          c_wealth "District wealth" ///
          c_petition "Petition support" ///
          c_elecsec "Electoral security" ///
          1.liberal#c.c_wealth "Liberal $\times$ Wealth" ///
          1.relright#c.c_petition "Religious $\times$ Petition" ///
          1.liberal#c.c_elecsec "Liberal $\times$ Elec. security" ///
          1.relright#c.c_elecsec "Religious $\times$ Elec. security" ///
          c.c_wealth#c.c_elecsec "Wealth $\times$ Elec. security" ///
          c.c_petition#c.c_elecsec "Petition $\times$ Elec. security" ///
          1.liberal#c.c_wealth#c.c_elecsec "Liberal $\times$ Wealth $\times$ Elec. security" ///
          1.relright#c.c_petition#c.c_elecsec "Religious $\times$ Petition $\times$ Elec. security" ///
          c_share_enfranchised "\% of men enfranchised" ///
          nobility "Aristocratic (1=yes)" ///
          incumbent "Incumbent (1=yes)" ///
          secure "Uncontested (1=yes)" ) ///
    refcat(c_tot_strikes "\textbf{Threat and Land}" c_share_enfranchised "\textbf{Controls}", nolab) ///
    stats(r2_p ll aic bic chi2 N, ///
          labels("Pseudo \text{R}^{2}" "Log-Likelihood" "AIC" "BIC" "Wald-Chi^{2}" "Observations") ///
          fmt(3 3 3 3 3 0)) ///
		  booktabs
				
*** create marginplots 


estimates clear 

eststo m_liberal: logit pro_tak ///
    i.liberal ///
    c.c_wealth ///
    c.c_elecsec ///
    i.liberal#c.c_wealth ///
    i.liberal#c.c_elecsec ///
    c.c_wealth#c.c_elecsec ///
    i.liberal#c.c_wealth#c.c_elecsec ///
    i.relright ///
    c.c_petition ///
    $controls, vce(robust)

margins, level(90) dydx(c_elecsec) ///
    at(liberal=(1) c_wealth=(-1(0.25)1.5)) ///
    noestimcheck
	
marginsplot, ///
    legend(position(6) size(medium)) ///
    ytitle("Marginal effect on Pr(Pro-Tak=1)", size(medium)) ///
    xtitle("Per capita personal tax revenue") ///
    xlabel(-1 "1" -0.5 "1.5" 0 "2" 0.5 "2.5" 1 "3" 1.5 "3.5", labsize(medium)) ///
    title("Liberals") ///
    saving(output/marginplot_wealth_elecsec.gph, replace)


eststo m_religious: logit pro_tak ///
    i.relright ///
    c.c_petition ///
    c.c_elecsec ///
    i.relright#c.c_petition ///
    i.relright#c.c_elecsec ///
    c.c_petition#c.c_elecsec ///
    i.relright#c.c_petition#c.c_elecsec ///
    i.liberal ///
    c.c_wealth ///
    $controls, vce(robust)	
	
margins, level(90) dydx(c_elecsec) ///
    at(relright=(1) c_petition=(-14(4)18)) ///
	noestimcheck

marginsplot, ///
    legend(position(6) size(medium)) ///
    ytitle("", size(medium)) ///
    xtitle("% signed 1878 petition") ///
    xlabel(-14 "0" -10 "4" -6 "8" -2 "12" 2 "16" 6 "20" 10 "24" 14 "28" 18 "32", labsize(medium)) ///
    title("Religious") ///
    saving(output/marginplot_petition_elecsec.gph, replace)

graph combine output/marginplot_wealth_elecsec.gph ///
    output/marginplot_petition_elecsec.gph, ///
    title("", size(medium)) ycommon

graph export output/marginplot_competitiveness.pdf, replace
